package com.ctz.commonframe.ui;

import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctz.commonframe.R;
import com.ctz.commonframe.utils.DensityUtil;
import com.ctz.commonframe.utils.Logger;

/**
 * 自定义标题栏的Activity
 * 
 * @author 陈堂正
 *
 */
public class TitleBarActivity extends BaseActivity {
//	private final static int HEIGHT_TITLE_BAR = 44;
	private final static int DEFAULT_TITLE_TEXT_SIZE = 18;// 单位是sp
	private final static int DEFAULT_TITLE_TEXT_COLOR = Color.WHITE;
	private final static int MARGIN_TITLE_BOTH_SIDE = 30;

	private ImageView ivLeft;
	private ImageView ivRight;
	private TextView titleTextView;
	private RelativeLayout titleBar;
	private LinearLayout decor;
	private TextView titleTextView2;
	private String titleText;
	private TextView tvRight;

	@Override
	public void setContentView(View view) {
		decor = new LinearLayout(this);
		decor.setBackgroundColor(getResources().getColor(R.color.sky_blue));
		decor.setClipToPadding(true);
		decor.setFitsSystemWindows(true);
		decor.setOrientation(LinearLayout.VERTICAL);
		View v = buildTitleBar();
        v.setFitsSystemWindows(true);
		decor.addView(v);
		decor.addView(view, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		super.setContentView(decor);
	}

	@Override
	public void setContentView(int layoutResID) {
		setContentView(getLayoutInflater().inflate(layoutResID, null));
	}

	private View buildTitleBar() {
		titleBar = new RelativeLayout(this);
		titleBar.setFocusable(true);
		titleBar.setFocusableInTouchMode(true);
		LinearLayout.LayoutParams lpTitleBar = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                (int)getResources().getDimension(R.dimen.title_bar_height));
		titleBar.setLayoutParams(lpTitleBar);
		titleBar.setBackgroundColor(getResources().getColor(android.R.color.transparent));

		ivLeft = new ImageView(this);
		ivLeft.setPadding(10, 15, 10, 15);
		ivLeft.setImageResource(R.drawable.image_back);
		RelativeLayout.LayoutParams lpBack = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lpBack.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		lpBack.addRule(RelativeLayout.CENTER_VERTICAL);
		lpBack.setMargins(MARGIN_TITLE_BOTH_SIDE, 0, MARGIN_TITLE_BOTH_SIDE, 0);
		ivLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleBar.addView(ivLeft, lpBack);

		titleTextView2 = new TextView(this);
		titleTextView2.setGravity(Gravity.CENTER);
		titleTextView2.setTextSize(DEFAULT_TITLE_TEXT_SIZE);
		titleTextView2.setTextColor(DEFAULT_TITLE_TEXT_COLOR);
		titleTextView2.setText(titleText == null ? "" : titleText);
		setTitleMiddleView(titleTextView2);

		ivRight = new ImageView(this);
		ivRight.setVisibility(View.GONE);
		RelativeLayout.LayoutParams lpRight = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lpRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		lpRight.addRule(RelativeLayout.CENTER_VERTICAL);
		lpRight.setMarginEnd(30);
		titleBar.addView(ivRight, lpRight);

		tvRight = new TextView(this);
		tvRight.setVisibility(View.GONE);
		tvRight.setTextSize(16);
		tvRight.setTextColor(DEFAULT_TITLE_TEXT_COLOR);
		titleBar.addView(tvRight, lpRight);

		return titleBar;
	}

	public void setTitleLeftRes(int resId) {
		ivLeft.setImageResource(resId);
	}

	public void setTitleLeftClickListener(OnClickListener listener) {
		ivLeft.setOnClickListener(listener);
	}

	public void setTitleRightClickListener(OnClickListener listener) {
		if(tvRight.getVisibility() == View.VISIBLE){
			Logger.e("title right is text");
			tvRight.setOnClickListener(listener);
		}else if(ivRight.getVisibility() == View.VISIBLE){
			Logger.e("title right is image");
			ivRight.setOnClickListener(listener);
		}
	}

	public void setTitleMiddleText(String titleText, int textSize, int textColor) {
		titleTextView2.setText(titleText);
	}

	public void setTitleMiddleText(String titleText) {
		setTitleMiddleText(titleText, DEFAULT_TITLE_TEXT_SIZE, DEFAULT_TITLE_TEXT_COLOR);
	}

	public void setTitleMiddleText(int stringID) {
		setTitleMiddleText(getResources().getString(stringID), DEFAULT_TITLE_TEXT_SIZE, DEFAULT_TITLE_TEXT_COLOR);
	}

	public void setTitleMiddleView(View view) {
		RelativeLayout.LayoutParams lpTitle = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lpTitle.addRule(RelativeLayout.CENTER_IN_PARENT);
		titleBar.addView(view, lpTitle);
	}

	public void setTitleMiddleView(View view, RelativeLayout.LayoutParams layoutParams) {
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		titleBar.addView(view, layoutParams);
	}

	public void setTitleMiddleView(int layout) {
		View v = LayoutInflater.from(this).inflate(layout, null);
		setTitleMiddleView(v);
	}

	public void setTitleTextComponudDrawable(int left, int top, int right, int bottom, int padding) {
		titleTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
		titleTextView.setCompoundDrawablePadding(10);
	}

	public void setTitleTextClickListener(OnClickListener listener) {
		titleTextView.setOnClickListener(listener);
	}

	public void setTitleBackEnable(boolean enabled) {
		ivLeft.setVisibility(enabled ? View.VISIBLE : View.GONE);

	}

	public void setTitleEnable(boolean enabled) {
		titleBar.setVisibility(enabled ? View.VISIBLE : View.GONE);
	}

	public void setTitleRightRes(int resId) {
		ivRight.setVisibility(View.VISIBLE);
		tvRight.setVisibility(View.GONE);
		ivRight.setImageResource(resId);

	}

	public void setTitleRightText(String str) {
		ivRight.setVisibility(View.GONE);
		tvRight.setVisibility(View.VISIBLE);
		tvRight.setText(str);
	}

	public void setTitleRightEnable(boolean enabled) {
		if(tvRight.isShown() && !ivRight.isShown()){
			tvRight.setEnabled(enabled);
		}else if(!tvRight.isShown() && ivRight.isShown()){
			tvRight.setEnabled(enabled);
		}
	}

	public void setBackgroundColor(int color){
		decor.setBackgroundColor(color);
		titleBar.setBackgroundColor(color);
	}

	public void setBackgroundResource(int resId){
		titleBar.setBackgroundResource(resId);
	}

	public int getTitleHeight() {
		return titleBar.getHeight();
	}

	public int getTitleBarHeight() {
		return titleBar.getHeight();
	}

	public View getTitleBarView() {
		return titleBar;
	}
	
	public void setTitleMiddleTextColor(int colorRes){
		titleTextView2.setTextColor(colorRes);
		tvRight.setTextColor(colorRes);
	}

	public View getTitleRightView () {
        if(ivRight.getVisibility() == View.VISIBLE)
            return ivRight;
        else if(tvRight.getVisibility() == View.VISIBLE)
            return tvRight;
        return null;
    }
}
